from src.laptop.preprocessor import prop_preprocessor as prop
from src.utils.config import logger
from src.utils.db_processor import mysql_prediction_processor

FISH_PRODUCT_SQL = """
SELECT fish_id, property, value
FROM laptop_fish_property 
"""

fish_product_mapping = {
    'S': 'level_bin_1',
    'A1': 'level_bin_2',
    'A2': 'level_bin_3',
    'A3': 'level_bin_4', 'A4': 'level_bin_4',
    'B1': 'level_bin_5',
    'B2': 'level_bin_6', 'B3': 'level_bin_6',
    'B4': 'level_bin_7', 'C1': 'level_bin_7', 'C2': 'level_bin_7',
    'D1': 'level_bin_8', 'D2': 'level_bin_8',
    'D3': 'level_bin_9', 'E1': 'level_bin_9', 'E2': 'level_bin_9',
    'F': 'level_bin_10', 'G': 'level_bin_11', 'H': 'level_bin_12', 'I': 'level_bin_13'
}


def load_fish_product():
    """
    读取钓鱼物品
    :return:
    """
    logger.info('loading fish products...')
    return mysql_prediction_processor.load_sql(FISH_PRODUCT_SQL)


def preprocess_fish_product(fish_product=None):
    """
    预处理钓鱼物品
    :param fish_product: 钓鱼物品原始数据，如果为None，那么先读取钓鱼物品原始数据
    :return:
    """
    if fish_product is None:
        fish_product = load_fish_product()
    fish_product_pivot = fish_product.pivot_table(index='fish_id', columns='property',
                                                  values='value', aggfunc=lambda x: x)
    fish_product_pivot = fish_product_pivot.reset_index()
    fish_product_pivot['memory'].replace(prop.memory_mapping, inplace=True)
    fish_product_pivot['ssd'].replace(prop.ssd_mapping, inplace=True)
    fish_product_pivot['hdd'].replace(prop.hhd_mapping, inplace=True)
    fish_product_pivot['level_bin'] = fish_product_pivot['product_level_name'].replace(fish_product_mapping)
    fish_product_pivot['CPU'] = fish_product_pivot['CPU'].map(lambda x: prop.rename_cpu(x))
    fish_product_pivot['GPU'] = fish_product_pivot['GPU'].map(lambda x: prop.rename_gpu(x))
    fish_product_pivot['keyboard'] = 'unknown'
    fish_product_pivot['color'] = 'unknown'
    fish_product_pivot['screen'] = 'unknown'

    return fish_product_pivot
